<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑品牌</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        var brandId = Ext.create('Ext.form.field.Hidden', {
          name: 'id'
        });
        
        var name = Ext.create('Ext.form.field.Text', {
          fieldLabel: '名称',
          allowBlank: false,
          blankText: "品牌名称不能为空",
          name: 'name',
          anchor: '95%'
        });

        //供应商
        var vendor = Ext.create('Ext.form.ComboBox', {
          store: vendorStore,
          fieldLabel: '供应商',
          allowBlank: false,
          blankText: "供应商必须选择",
          valueField: 'id',
          displayField: 'name',
          name: 'vendor_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',          
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,          
          readOnly:false,
          minChars: 0,
          editable:false
        });
        
        var desc = Ext.create('Ext.form.TextArea', {
          fieldLabel: '描述',
          name: 'description',
          height:80,
          anchor: '95%'
        });

        //提交按钮
        var submitButton = Ext.create('Ext.Button', {
          text: '提交',
          iconCls: 'icon-save',
          handler: function(){
            if (editBrandForm.form.isValid()) {
              editBrandForm.form.submit({
                method: 'GET',
                success: function(result, response){
                  if (response.result.info == "成功") {
                    //添加成功后，隐藏窗口，并刷新Grid
                    editBrandWin.destroy();
                  }
                  else {
                    Ext.MessageBox.alert('消息', response.result.info);
                  }
                },
                failure: function(result, response){
                  Ext.MessageBox.alert('提示', result.responseText);
                }
              });
            }
            else {
              Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
            }
          }
        })

        //暂时保存按钮
        var closeButton = Ext.create('Ext.Button', {
          text: '关闭',
          tooltip: '关闭当前窗口，并且当前编辑的信息不能保存。',
          iconCls: 'exit',
          handler: function(){
            editBrandWin.destroy();
          }
        });
        
        var editBrandForm = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 55,
            labelAlign: 'right'
          },
          frame:true,
          url: '/brand/update?id=' + brandId,
          reader: Ext.create('Ext.data.reader.Json',{
            model: 'BrandModel',
            root: 'root'
          }),
          bodyStyle:'padding:5px 5px 0',
          width: 310,
          //height:300,
          items: [brandId,name,vendor,desc],
          buttons: [submitButton,closeButton]
        });
        //编辑前，把数据引入到表单中。
        // load form and assign value to fields
        vendorStore.load({callback: function(record, options, success){
            if(success){
              editBrandForm.form.load({
                url: '/brand/getBrandById?id=<%=params[:id]%>',
                method : 'GET',
                waitMsg: '正在载入数据...',
                success: function(form, action){
                },
                failure: function(form, action){
                  Ext.MessageBox.alert('提示信息', '信息加载失败');
                }
              });
            }else{
              Ext.MessageBox.alert('操作','失败！');
            }
          }});
            
        editBrandForm.render('edit_brand_form');
      })
    </script>
  </head>
  <body>
    <div id="edit_brand_form"></div>
  </body>
</html>
